package com.mystudy.leetcode.problem.tree.p_783;

import com.mystudy.leetcode.base.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 周志龙
 * @Email 15216876513@163.com
 * @Date on 2019/7/27 19:18
 * @Description 二叉搜索树结点最小距离
 */
public class Solution {
    public int minDiffInBST(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        inOrder(root,result);
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < result.size()-1; i++) {
           int minVal= result.get(i + 1) - result.get(i);
           if(minVal<min){
               min = minVal;
           }
        }
        return min;
    }

    private void inOrder(TreeNode node,List list){
        if(node == null){
            return;
        }
        inOrder(node.left,list);
        list.add(node.val);
        inOrder(node.right,list);
    }
}
